package com.eliza.base.views.database.broadcast

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.eliza.base.broadcast.BroadcastReceiverReceiver
import com.eliza.base.broadcast.MyBroadcastReceiver
import com.eliza.base.broadcast.MyBroadcastReceiverPackage


@Composable
fun BroadcastScreen(nav: NavController) {
    val activity = LocalContext.current as Activity
    val myBroadcastReceiver: MyBroadcastReceiver = MyBroadcastReceiver(activity)
    Log.e("BroadcastScreen", "BroadcastScreen: activity :${activity}")
    Column(modifier = Modifier.padding(10.dp)) {
        Row(
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically,
        ) {
            var checked by remember { mutableStateOf(false) }
            var isReceive by remember { mutableStateOf(false) }
            Text(text = "register")
            Switch(
                checked = checked,
                onCheckedChange = {
                    checked = it
                    if (checked) {
                        //動態註冊 接收器接收的事件
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                            activity.applicationContext.registerReceiver(
                                myBroadcastReceiver,
                                BroadcastReceiverReceiver,
                                Context.RECEIVER_EXPORTED
                            )
                            Toast.makeText(
                                activity,
                                "registerReceiver Build.VERSION: ${Build.VERSION.SDK_INT}",
                                Toast.LENGTH_SHORT
                            ).show()
                            isReceive = true
                        }
                    } else {
                        if (isReceive) {
                            activity.applicationContext.unregisterReceiver(myBroadcastReceiver) // 发送广播
                            Toast.makeText(activity, "unregisterReceiver", Toast.LENGTH_SHORT)
                                .show()
                        }

                    }
                }
            )
        }
        TextButton(onClick = {
            val intent = Intent(MyBroadcastReceiverPackage).apply {
                putExtra("data", "Notice me senpai!")
            }
            activity.sendBroadcast(intent) // 发送广播
        }) {
            Text(text = "Click To Send")
        }

    }
}